<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/8/17
 * Time: 17:22
 */

namespace app\modules\controllers;

use app\modules\models\Rbac;
use Yii;
use app\modules\models\Admin;
use yii\data\Pagination;
use yii\web\NotFoundHttpException;

class ManageController extends CommonController
{
    protected $mustLogin = ['assign','mailchangepass','managers','reg','del','changeemail','changepass'];
    public function actionMailchangepass()
    {
        $this->layout = false;
        $time = Yii::$app->request->get("timestamp");
        $adminuser = Yii::$app->request->get("adminuser");
        $token = Yii::$app->request->get("token");
        $model = new Admin;
        $myToken = $model->createToken($adminuser, $time);
        if ($token != $myToken) {
            $this->redirect(['public/login']);
            Yii::$app->end();
        }
        if (time() - $time > 300) {
            $this->redirect(['public/login']);
            Yii::$app->end();
        }
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            if ($model->changePass($post)) {
                Yii::$app->session->setFlash('info', '密码修改成功');
            }
        }
        $model->adminuser = $adminuser;
        return $this->render("mailchangepass", ['model' => $model]);

    }

    public function actionManagers()
    {
        $this->layout = "layout1";
        $model = Admin::find();
        $count = count($model);
        $pages = new Pagination(['pageSize' => 10,'totalCount' => $count]);
        $managers = $model->offset($pages->offset)->limit($pages->limit)->all();
        return $this->render("managers",['managers' => $managers,'pager' => $pages]);
    }

    public function actionReg()
    {
        $this->layout = 'layout1';
        $model = new Admin();
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            if ($model->reg($post)) {
                Yii::$app->session->setFlash('info','添加成功');
            } else {
                Yii::$app->session->setFlash('info','添加失败');
            }
        }
        $model->adminpass = '';
        $model->repass = '';
        return $this->render('reg',['model' => $model]);
    }

    public function actionDel()
    {
        $adminid = (int)Yii::$app->request->get('adminid');
        if (empty($adminid) || $adminid == 1) {
            $this->redirect(['manage/managers']);
            return false;
        }
        $model = new Admin();
        if ($model->deleteAll(['adminid' => $adminid])){
            Yii::$app->session->setFlash('info','删除成功');
            $this->redirect(['manage/managers']);
        }
    }

    public function actionChangeemail()
    {
        $this->layout = 'layout1';
        $model = Admin::find()->where(['adminuser' => Yii::$app->session['admin']['adminuser']])->one();
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            if ($model->changeemail($post)) {
                Yii::$app->session->setFlash('info','修改成功');
            }
        }
        $model->adminpass = "";
        return $this->render('changeemail',['model' => $model]);
    }

    public function actionChangepass()
    {
        $this->layout = "layout1";
        $model = Admin::find()->where(['adminuser' => Yii::$app->session['admin']['adminuser']])->one();
        if (Yii::$app->request->isPost) {
            if ($model->changepass(Yii::$app->request->post())) {
                Yii::$app->session->setFlash('info','修改成功');
            }
        }
        $model->adminpass = '';
        $model->repass = '';
        return $this->render('changepass',['model' => $model]);
    }

    public function actionAssign($adminid)
    {
        $adminid = (int)$adminid;
        if (empty($adminid)) {
            throw new \Exception("参数错误");
        }
        $admin = Admin::findOne($adminid);
        if (empty($admin)) {
            throw new NotFoundHttpException("页面不存在");
        }

        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            $children = !empty($post['children'])?:[];
            if (Rbac::grant($adminid,$children)) {
                Yii::$app->session->setFlash('info','授权成功');
            }
        }

        $auth = Yii::$app->authManager;
        $roles = Rbac::getOptions($auth->getRoles(),null);
        $permissions = Rbac::getOptions($auth->getPermissions(),null);
        $children = Rbac::getChildrenByUser($adminid);
        return $this->render('_assign',['children' => $children, 'roles' => $roles, 'permissions' => $permissions, 'admin' => $admin->adminuser]);
    }
}